from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from time import sleep

# 指定chromedriver路径（137版本）
driver_path = "D:\\pycharm\\PM_test\\chromedriver.exe"

# 创建WebDriver实例
service = Service(driver_path)
options = Options()
browser = webdriver.Chrome(service=service, options=options)
browser.maximize_window()
wait = WebDriverWait(browser, 10)

# 登录函数
def login():
    browser.get("http://146.56.217.9:9131")
    browser.find_element(By.ID, "username").send_keys("admin_9131")
    browser.find_element(By.ID, "password").send_keys("admin")
    browser.find_element(By.CLASS_NAME, "button").click()

# 用户搜索函数：所有参数都有默认值，可选填
def user_search(login_name="", lastname="", firstname="", phone_pro="", phone_mobile="", status_index=0):
    """
    封装后的用户搜索功能
    参数：
        login_name: 登录名
        lastname: 姓氏
        firstname: 名字
        phone_pro: 商务电话
        phone_mobile: 手机
        status_index: 用户状态（0表示不选择，2为禁用，3为生效）
    """
    # 点击菜单进入用户页面
    browser.find_element(By.CSS_SELECTOR, ".menu_titre > a[title='用户和组']").click()
    browser.find_element(By.CSS_SELECTOR, ".menu_contenu_user_list > a[title='用户列表']").click()

    # 填写搜索条件
    browser.find_element(By.NAME, "search_login").send_keys(login_name)
    browser.find_element(By.NAME, "search_lastname").send_keys(lastname)
    browser.find_element(By.NAME, "search_firstname").send_keys(firstname)
    browser.find_element(By.NAME, "search_phonepro").send_keys(phone_pro)
    browser.find_element(By.NAME, "search_phonemobile").send_keys(phone_mobile)

    # 设置状态（如果提供了有效index）
    if status_index in [2, 3]:
        browser.find_element(By.CSS_SELECTOR, f"select#search_status > option:nth-child({status_index})").click()

    # 点击搜索按钮
    browser.find_element(By.NAME, "button_search_x").click()


# 测试用例列表，每个元素是一个字典，对应一组搜索条件
#运行结果，全部可以搜到
test_cases = [
    {"login_name": "WM01", "lastname": "", "firstname": "", "phone_pro": "", "phone_mobile": "", "status_index": 0},
    {"login_name": "", "lastname": "Zhang", "firstname": "San", "phone_pro": "", "phone_mobile": "", "status_index": 0},
    {"login_name": "", "lastname": "", "firstname": "", "phone_pro": "", "phone_mobile": "13789012345", "status_index": 0},
    {"login_name": "", "lastname": "", "firstname": "", "phone_pro": "0755-88886666", "phone_mobile": "", "status_index": 0},
    {"login_name": "ZS01", "lastname": "", "firstname": "", "phone_pro": "", "phone_mobile": "", "status_index": 0}
]

# 主程序入口
if __name__ == '__main__':
    try:
        login()
        sleep(1)  # 等待页面加载

        for index, case in enumerate(test_cases):
            print(f"\n执行第 {index + 1} 个用例：{case}")
            user_search(**case)
            sleep(5)  # 等待搜索结果加载
            browser.back()  # 返回搜索页面以便下次继续搜索
            sleep(1)

    except Exception as e:
        print("发生错误：", e)

    finally:
        sleep(1)
        browser.quit()
